package com.yz.bi.controller;

import com.yz.bi.service.BiService;
import com.yz.common.pojo.Database;
import com.yz.common.pojo.DbLink;
import com.yz.common.utils.Result;
import com.yz.common.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 段成
 * @date 2019/12/12 - 10:07
 */
@RestController
@RequestMapping("/biController")
@CrossOrigin
public class BiController {

    @Autowired
    private BiService biService;

    /**
     * 获取所有的连接
     * @return
     */
    @GetMapping("/getLinkName")
    public Result getLinkName(){

        List<DbLink> linkNameList = biService.getLinkName();
        if(linkNameList!=null){
            return new Result(true, StatusCode.OK,"查询成功",linkNameList,"获取所有的连接");
        }
        return new Result(false, StatusCode.ERROR,"查询失败","获取所有的连接");

    }

    /**
     * 根据选择的连接获取该连接下所有数据库
     * @param linkId
     * @return
     */
    @GetMapping("/getDatabaseByLinkId/{linkId}")
    public Result getDatabaseByLinkId(@PathVariable("linkId") Integer linkId){
        List<Database> databaseList = biService.getDatabaseByLinkId(linkId);
        if(databaseList!=null){
            return new Result(true, StatusCode.OK,"查询成功",databaseList,"根据选择的连接获取id为"+linkId+"连接下所有数据库");
        }
        return new Result(false, StatusCode.ERROR,"查询失败","根据选择的连接获取id为"+linkId+"连接下所有数据库");
    }

    /**
     * 根据数据库id获取该数据库下所有表名
     * @param databaseId
     * @return
     */
    @GetMapping("/getTablesByDatabaseId/{databaseId}")
    public Result getTablesByDatabaseId(@PathVariable("databaseId") Integer databaseId){

        List<String> tableList = biService.getTablesByDatabaseId(databaseId);
        System.out.println(tableList.toString());
        if(tableList!=null){
            return new Result(true, StatusCode.OK,"查询成功",tableList,"根据数据库id:"+databaseId+"获取该数据库下所有表名");
        }
        return new Result(false, StatusCode.ERROR,"查询失败","根据数据库id:"+databaseId+"获取该数据库下所有表名");
    }

    /**
     * 查询表的总记录数,去前端生成总页数,
     * @param databaseId
     * @param tableName
     * @return
     */
    @GetMapping("/getTotalByDatabaseNameAndTableName/{databaseId}/{tableName}")
    public Result getTotalByDatabaseNameAndTableName(@PathVariable("databaseId") Integer databaseId,
                                                    @PathVariable("tableName") String tableName){
        int count = biService.getTotalByDatabaseNameAndTableName(databaseId,tableName);

        if(count!=-1){
            return new Result(true, StatusCode.OK,"查询成功",count,"查询表的总记录数");
        }
        return new Result(false, StatusCode.ERROR,"查询失败","查询表的总记录数");
    }

    /**
     * 根据表名和数据库id获取该表数据
     * @param databaseId
     * @param tableName
     * @return
     */
    @GetMapping("/getListByDatabaseNameAndTableName/{databaseId}/{tableName}/{pageNum}")
    public Result getListByDatabaseNameAndTableName(@PathVariable("databaseId") Integer databaseId,
                                                          @PathVariable("tableName") String tableName,
                                                    @PathVariable("pageNum") Integer pageNum){
        List<Map> list = biService.getListByDatabaseNameAndTableName(databaseId,tableName,pageNum);

        if(list!=null){
            return new Result(true, StatusCode.OK,"查询成功",list,"获取表数据");
        }
        return new Result(false, StatusCode.ERROR,"查询失败","获取表数据");
    }


}
